package com.onlyphoto.onlymall.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import com.onlyphoto.onlymall.BuildConfig;
import com.onlyphoto.onlymall.MyApplication;
import com.onlyphoto.onlymall.bean.response.UserInfoBean;

public class ConfigUtil {

    private Context context;

    private String channelName = "vivo";  //渠道号

    public static final String KEY_COOKIE_LIST = "cookielist";

    public ConfigUtil(Context context) {
        this.context = context;
        setUserInfo(getUserInfo());
    }

    @SuppressLint("StaticFieldLeak")
    public static volatile ConfigUtil instance = null;

    public static ConfigUtil init(Context context) {
        if (null == instance) {
            synchronized (SharePreferenceUtil.class) {
                if (null == instance) {
                    instance = new ConfigUtil(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    //获取用户当前登录状态
    public boolean getLoginStatus() {
        return !TextUtils.isEmpty(SharePreferenceUtil.getInstance(MyApplication.Companion.getApp()).getToken());
    }

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    public UserInfoBean getUserInfo() {
        return ConvertUtil.toObject(SharePreferenceUtil.getInstance(context).getData(Constants.SHARE_USER_INFO), UserInfoBean.class);
    }

    public void setUserInfo(UserInfoBean userInfo) {
        SharePreferenceUtil.getInstance(context).setData(Constants.SHARE_USER_INFO, ConvertUtil.toJsonString(userInfo));
    }

    public String getUrl(String url){
        if (StringUtil.isBlank(url)) {
            return "";
        }
        String ret_url;
        if (url.contains("?")) {
            ret_url = url + "&";
        } else {
            ret_url = url + "?";
        }
        ret_url += "apikey=" + BuildConfig.API_KEY
                + "&platform=3"
                + "&msg_type=1"
                + "&appMarket=" + ConfigUtil.instance.getChannelName();
        return ret_url.replace(" ", "");
    }
}
